package cn.tataben.database;

import android.util.Log;

/**
 * 日志
 * @author tataben
 */
public class Logger {

    private Logger() {}

    private static String TAG = "";
    private static boolean DEBUG = false;
    public static void setTag(String tag) {
        TAG = tag;
    }
    public static void setDebug(boolean debug) {
        DEBUG = debug;
    }

    private static String generate() {
        StackTraceElement caller = new Throwable().getStackTrace()[2];

        String tag = "%s %s.%s(L:%d)";
        String className = caller.getClassName();
        className = className.substring(className.lastIndexOf(".") + 1);
        return String.format(tag, TAG, className, caller.getMethodName(), caller.getLineNumber());
    }

    public static void d(String content) {
        if (DEBUG) {
            String tag = generate();
            Log.d(tag, content);
        }
    }
    public static void d(String content, Throwable tr) {
        if (DEBUG) {
            String tag = generate();
            Log.d(tag, content, tr);
        }
    }

    public static void e(String content) {
        if (DEBUG) {
            String tag = generate();
            Log.e(tag, content);
        }
    }
    public static void e(String content, Throwable tr) {
        if (DEBUG) {
            String tag = generate();
            Log.e(tag, content, tr);
        }
    }

    public static void i(String content) {
        if (DEBUG) {
            String tag = generate();
            Log.i(tag, content);
        }
    }
    public static void i(String content, Throwable tr) {
        if (DEBUG) {
            String tag = generate();
            Log.i(tag, content, tr);
        }
    }

    public static void w(String content) {
        if (DEBUG) {
            String tag = generate();
            Log.w(tag, content);
        }
    }
    public static void w(String content, Throwable tr) {
        if (DEBUG) {
            String tag = generate();
            Log.w(tag, content, tr);
        }
    }

}